package com.ruoyi.channel.controller;

import com.ruoyi.channel.domain.bo.PushOperate;
import com.ruoyi.channel.domain.bo.TChannelPushDisBo;
import com.ruoyi.channel.domain.vo.ChannelCustomerInfoVo;
import com.ruoyi.channel.domain.vo.ChannelPushDisVo;
import com.ruoyi.channel.domain.vo.TChannelPushDisVo;
import com.ruoyi.channel.service.ITChannelPushDisService;
import com.ruoyi.channelApi.domain.CfCrmRequest;
import com.ruoyi.channelApi.domain.ChannelRequest;
import com.ruoyi.channelApi.domain.vo.TChannelPushVo;
import com.ruoyi.channelApi.service.ITChannelPushService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.JsonUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.enums.ChannelPushEnum;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 渠道数据分配
 *
 * @author ruoyi
 * @date 2023-11-02
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/channel/channelPushDis")
public class TChannelPushDisController extends BaseController {

    private final ITChannelPushDisService iTChannelPushDisService;

    private final ITChannelPushService iTChannelPushService;

    /**
     * 查询渠道数据分配列表
     */
    //@SaCheckPermission("channel:channelPushDis:list")
    @GetMapping("/list")
    public TableDataInfo<ChannelPushDisVo> list(TChannelPushDisBo bo, PageQuery pageQuery) {
        return iTChannelPushDisService.queryPageDisList(bo, pageQuery);
    }

    /**
     * 导出渠道数据分配列表
     */
    //@SaCheckPermission("channel:channelPushDis:export")
    @Log(title = "渠道数据分配", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TChannelPushDisBo bo, HttpServletResponse response) {
        List<TChannelPushDisVo> list = iTChannelPushDisService.queryList(bo);
        ExcelUtil.exportExcel(list, "渠道数据分配", TChannelPushDisVo.class, response);
    }

    /**
     * 获取渠道数据分配详细信息
     *
     * @param id 主键
     */
    //@SaCheckPermission("channel:channelPushDis:query")
    @GetMapping("/{id}")
    public R<ChannelPushDisVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        ChannelPushDisVo channelPushDisVo = new ChannelPushDisVo();
        TChannelPushDisVo tChannelPushDisVo = iTChannelPushDisService.queryById(id);
        String channelSource = tChannelPushDisVo.getChannelSource();
        Long channelPushId = tChannelPushDisVo.getChannelPushId();
        TChannelPushVo tChannelPushVo = iTChannelPushService.queryById(channelPushId);

        channelPushDisVo.setChannelSource(channelSource);
        if (ChannelPushEnum.CRM_CHANNEL.getValue().equals(channelSource)) {
            ChannelRequest channelRequest = JsonUtils.parseObject(tChannelPushVo.getDataInfo(), ChannelRequest.class);
            channelPushDisVo.setChannelRequest(channelRequest);
        } else if (ChannelPushEnum.CF_CRM_CHANNEL.getValue().equals(channelSource)) {
            CfCrmRequest crmRequest = JsonUtils.parseObject(tChannelPushVo.getDataInfo(), CfCrmRequest.class);
            channelPushDisVo.setCfCrmRequest(crmRequest);
        } else {
            ChannelRequest channelRequest = JsonUtils.parseObject(tChannelPushVo.getDataInfo(), ChannelRequest.class);
            channelPushDisVo.setChannelRequest(channelRequest);
        }
        return R.ok(channelPushDisVo);
    }

    /**
     * 新增渠道数据分配
     */
    //@SaCheckPermission("channel:channelPushDis:add")
    @Log(title = "渠道数据分配", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TChannelPushDisBo bo) {
        return toAjax(iTChannelPushDisService.insertByBo(bo));
    }

    /**
     * 修改渠道数据分配
     */
    //@SaCheckPermission("channel:channelPushDis:edit")
    @Log(title = "渠道数据分配", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TChannelPushDisBo bo) {
        return toAjax(iTChannelPushDisService.updateByBo(bo));
    }

    /**
     * 删除渠道数据分配
     *
     * @param ids 主键串
     */
    //@SaCheckPermission("channel:channelPushDis:remove")
    @Log(title = "渠道数据分配", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iTChannelPushDisService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    @PostMapping("/pushOperate")
    public R<Void> pushDis(@RequestBody PushOperate pushOperate) {
        iTChannelPushDisService.pushOperate(pushOperate);
        return R.ok();
    }

    @PostMapping("/getChannelCustomerList")
    public R<List<ChannelCustomerInfoVo>> chnanelCustomer(@RequestBody PushOperate pushOperate) {
        return R.ok(iTChannelPushDisService.getChannelCustomerList(pushOperate));
    }
}
